/*
 * DogeScanWindow.java
 *
 * Created on 15 luglio 2007, 13.03
 */

package dogeUI;

import dogeCore.*;
import dogeScan.*;
import java.awt.Image;
import java.io.IOException;
import javax.accessibility.Accessible;

/**
 *
 * @author  daniele
 */
public class DogeScanWindow extends javax.swing.JFrame implements Accessible {
    
    /**
     * Creates new form DogeScanWindow
     */
    public DogeScanWindow(DogeCore core) {
        this.core=core;
        //now scanner is created statically, but may be created "dinamically" following a config file
        core.setScanner(new DogeFakeScanner());
        initComponents();
        //set default button (in the beginning this is "acquire new page" button
        getRootPane().setDefaultButton(scanNewPageButton);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        confirmPanel = new javax.swing.JPanel();
        confirmButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        controlPanel = new javax.swing.JPanel();
        scanLabel = new javax.swing.JLabel();
        scanNewPageButton = new javax.swing.JButton();
        scanLastPageButton = new javax.swing.JButton();
        pagesLabel = new javax.swing.JLabel();
        helpLabel = new javax.swing.JLabel();
        previewScrollPane = new javax.swing.JScrollPane();
        previewLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("DoGe - Modulo di acquisizione dei documenti");
        setMinimumSize(new java.awt.Dimension(697, 400));
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        confirmPanel.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        confirmPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        confirmButton.setFont(new java.awt.Font("Dialog", 1, 14));
        confirmButton.setText("Inserisci scansione");
        confirmButton.setEnabled(false);
        confirmButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                confirmButtonActionPerformed(evt);
            }
        });

        confirmPanel.add(confirmButton);

        cancelButton.setFont(new java.awt.Font("Dialog", 1, 14));
        cancelButton.setText("Annulla");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        confirmPanel.add(cancelButton);

        getContentPane().add(confirmPanel, java.awt.BorderLayout.SOUTH);

        controlPanel.setLayout(new java.awt.GridBagLayout());

        controlPanel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        scanLabel.setFont(new java.awt.Font("Dialog", 1, 14));
        scanLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        scanLabel.setText("Scansiona...");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        controlPanel.add(scanLabel, gridBagConstraints);

        scanNewPageButton.setFont(new java.awt.Font("Dialog", 1, 14));
        scanNewPageButton.setText("Nuova pagina...");
        scanNewPageButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                scanNewPageButtonActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        controlPanel.add(scanNewPageButton, gridBagConstraints);

        scanLastPageButton.setFont(new java.awt.Font("Dialog", 1, 14));
        scanLastPageButton.setText("Ultima pagina...");
        scanLastPageButton.setEnabled(false);
        scanLastPageButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        scanLastPageButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                scanLastPageButtonActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        controlPanel.add(scanLastPageButton, gridBagConstraints);

        pagesLabel.setFont(new java.awt.Font("Dialog", 1, 14));
        pagesLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        pagesLabel.setText("Pagine scansionate: 0");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        controlPanel.add(pagesLabel, gridBagConstraints);

        getContentPane().add(controlPanel, java.awt.BorderLayout.EAST);

        helpLabel.setFont(new java.awt.Font("Dialog", 1, 14));
        helpLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        helpLabel.setText("<html>\n<center>\nScegliere <i>Nuova pagina...</i> dalla colonna a destra per acquisire una nuova pagina, oppure <i>Questa pagina...</i> per sostituire l'ultima pagina acquisita.<br>\nQuando \u00e8 stato acquisito l'intero documento scegliere <i>Inserisci scansione</i> per confermare e chiudere questa finestra.</center></html>");
        helpLabel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        getContentPane().add(helpLabel, java.awt.BorderLayout.NORTH);

        previewScrollPane.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        previewLabel.setFont(new java.awt.Font("Dialog", 0, 14));
        previewLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        previewLabel.setText("(qui verr\u00e0 visualizzata la pagina scansionata)");
        previewScrollPane.setViewportView(previewLabel);

        getContentPane().add(previewScrollPane, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void scanLastPageButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_scanLastPageButtonActionPerformed
        rescanLastPage();
    }//GEN-LAST:event_scanLastPageButtonActionPerformed

    private void scanNewPageButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_scanNewPageButtonActionPerformed
        scanNewPage();
    }//GEN-LAST:event_scanNewPageButtonActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        terminateScanWindow();
    }//GEN-LAST:event_formWindowClosing

    private void confirmButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_confirmButtonActionPerformed
        saveScanAndTerminate();
    }//GEN-LAST:event_confirmButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        terminateScanWindow();
    }//GEN-LAST:event_cancelButtonActionPerformed

    /** terminate scan window and deallocate scanner from DogeCore */
    private void terminateScanWindow() {
        //deallocate scanner form core
        core.setScanner(null);
        //close window
        dispose();

    }

    /** save scanned document in DogeCore and terminates DogeScan*/
    private void saveScanAndTerminate() {
        //save scanned image
        core.saveScannedImage();
        //terminate scan window
        terminateScanWindow();
    }

    /** scan a new page */
    private void scanNewPage() {
        try {
            //scan a new page
            core.scanNewPage();
            //get preview of scanned page
            previewLabel.setText("");
            previewLabel.setIcon(new javax.swing.ImageIcon(core.getPreview()));
            //get number of scanned pages
            int scannedPages=core.getScannedPages();
            //update info label
            pagesLabel.setText("Pagine scansionate: "+scannedPages);
            //if this is the case enable "Rescan Last Page" button and "Insert scan"
            if (scannedPages>0){
                scanLastPageButton.setEnabled(true);
                confirmButton.setEnabled(true);
                //if there is at leas one page already scanned the new default button is "insert scan"
                getRootPane().setDefaultButton(confirmButton);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    /** rescan last scanned page */
    private void rescanLastPage() {
        try {
            //scan again last page
            core.rescanLastPage();
            //get preview of scanned page
            previewLabel.setText("");
            previewLabel.setIcon(new javax.swing.ImageIcon(core.getPreview()));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton confirmButton;
    private javax.swing.JPanel confirmPanel;
    private javax.swing.JPanel controlPanel;
    private javax.swing.JLabel helpLabel;
    private javax.swing.JLabel pagesLabel;
    private javax.swing.JLabel previewLabel;
    private javax.swing.JScrollPane previewScrollPane;
    private javax.swing.JLabel scanLabel;
    private javax.swing.JButton scanLastPageButton;
    private javax.swing.JButton scanNewPageButton;
    // End of variables declaration//GEN-END:variables
    private DogeCore core;
}
